/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.brands.application.ics.dao;

import br.com.brands.application.ics.beans.BrandsBeans;
import br.com.brands.application.ics.dao.intf.GenericDAOIntf;
import br.com.brands.application.ics.dao.util.GenericDAOService;
import java.io.Serializable;
import java.util.List;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.LockOptions;
import org.hibernate.Session;

/**
 *
 * @author ppacheco
 */
public abstract class GenericDAOImpl<T, ID extends Serializable> implements
        GenericDAOIntf<Object, Serializable> {

    /**
     *
     */
    private static final long serialVersionUID = -6177093567847567610L;
    private static final Logger log = Logger.getLogger(GenericDAOImpl.class
            .getName());
    private Session session = null;

    public GenericDAOImpl() {
        log.log(Level.INFO, "Loading GenericDAOImpl.");
    }

    @SuppressWarnings("unchecked")
    @Override
    public ID save(Object t) throws HibernateException, InstantiationException {

        ID id = null;

        try {
            if (t != null) {
                session = GenericDAOService.getInstance().getSession(session);
                log.log(Level.INFO, ">>> Estou passando por GenericDAOImpl");
                BrandsBeans brandsBeans = (BrandsBeans) t;
                log.log(Level.INFO, "Brands ID = " + brandsBeans.getBrandsID());
                log.log(Level.INFO, "Brands Name = " + brandsBeans.getBrandsName());
                id = (ID) session.save(t);
            }
        } catch (InstantiationException e) {
            log.log(Level.ERROR, e.getMessage(), e);
        } catch (HibernateException e) {
            log.log(Level.ERROR, e.getMessage(), e);
        } finally {
            try {
                if (session != null) {
                    GenericDAOService.getInstance().closeSession(session);
                }
            } catch (InstantiationException e) {
                log.log(Level.ERROR, e.getMessage(), e);
            } catch (HibernateException e) {
                log.log(Level.ERROR, e.getMessage(), e);
            }
        }
        return id;
    }

    @Override
    public void saveOrUpdate(Object t) throws HibernateException,
            InstantiationException {

        try {
            session = GenericDAOService.getInstance().getSession(session);
            if (t != null) {
                log.log(Level.INFO, ">>> Estou passando por GenericDAOImpl");
                BrandsBeans brandsBeans = (BrandsBeans) t;
                log.log(Level.INFO, "Brands ID = " + brandsBeans.getBrandsID());
                log.log(Level.INFO, "Brands Name = " + brandsBeans.getBrandsName());
                session.saveOrUpdate(brandsBeans);
            }

        } catch (InstantiationException e) {
            log.log(Level.ERROR, e.getMessage(), e);
        } catch (HibernateException e) {
            log.log(Level.ERROR, e.getMessage(), e);
        } finally {
            try {
                if (session != null) {
                    GenericDAOService.getInstance().closeSession(session);
                }
            } catch (InstantiationException e) {
                log.log(Level.ERROR, e.getMessage(), e);
            } catch (HibernateException e) {
                log.log(Level.ERROR, e.getMessage(), e);
            }
        }
    }

    @Override
    public void delete(Object t) throws HibernateException,
            InstantiationException {

        try {
            session = GenericDAOService.getInstance().getSession(session);
            session.delete(t);
        } catch (InstantiationException e) {
            log.log(Level.ERROR, e.getMessage(), e);
        } catch (HibernateException e) {
            log.log(Level.ERROR, e.getMessage(), e);
        } finally {
            try {
                if (session != null) {
                    GenericDAOService.getInstance().closeSession(session);
                }
            } catch (InstantiationException e) {
                log.log(Level.ERROR, e.getMessage(), e);
            } catch (HibernateException e) {
                log.log(Level.ERROR, e.getMessage(), e);
            }
        }
    }

    @Override
    public Object findByID(Object t, Serializable id, boolean lock)
            throws HibernateException, InstantiationException {

        Object o = null;

        try {

            session = GenericDAOService.getInstance().getSession(session);

            if (lock) {
                o = (Object) session.load(t.getClass(), id, LockOptions.READ);
            } else {
                o = (Object) session.load(t.getClass(), id);
            }
            session.delete(t);
        } catch (InstantiationException e) {
            log.log(Level.ERROR, e.getMessage(), e);
        } catch (HibernateException e) {
            log.log(Level.ERROR, e.getMessage(), e);
        } finally {
            try {
                if (session != null) {
                    GenericDAOService.getInstance().closeSession(session);
                }
            } catch (InstantiationException e) {
                log.log(Level.ERROR, e.getMessage(), e);
            } catch (HibernateException e) {
                log.log(Level.ERROR, e.getMessage(), e);
            }
        }
        return o;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Object> findAll(Object t) throws HibernateException,
            InstantiationException {

        List<Object> list = null;

        try {
            session = GenericDAOService.getInstance().getSession(session);
            Criteria criteria = (Criteria) session.createCriteria(t.getClass());
            list = (List<Object>) criteria.list();
        } catch (InstantiationException e) {
            log.log(Level.ERROR, e.getMessage(), e);
        } catch (HibernateException e) {
            log.log(Level.ERROR, e.getMessage(), e);
        } finally {
            try {
                if (session != null) {
                    GenericDAOService.getInstance().closeSession(session);
                }
            } catch (InstantiationException e) {
                log.log(Level.ERROR, e.getMessage(), e);
            } catch (HibernateException e) {
                log.log(Level.ERROR, e.getMessage(), e);
            }
        }
        return list;
    }
}
